package com.starbaba.user.setinfo.view;

import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.user.setinfo.model.UserInfoSetModel;
import com.starbaba.user.setinfo.service.UserInfoSetService;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings({"rawtypes", "unchecked", "serial"})
public class UserInfoSetAction extends BaseAction implements ModelDriven<UserInfoSetModel>{

	private UserInfoSetModel model = null;
	
	private UserInfoSetService service;
	
	private InputStream input;

	private String fileName;
	
	public InputStream getInput() {
		return input;
	}

	public void setInput(InputStream input) {
		this.input = input;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	@Autowired
	@Qualifier("userInfoSetService")
	public void setService(UserInfoSetService service) {
		this.service = service;
	}

	public String showPage() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		try {
			service.showPage(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ActionSupport.SUCCESS;
	}

	public void queryList() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();

		try {
			List<UserInfoSetModel> list = new ArrayList<UserInfoSetModel>();
			list = service.getList(start, length, pageResponse, paramsMap);
			//手机号中间四位转为*
			for (int i = 0; i <list.size(); i++) {
				UserInfoSetModel bi = list.get(i);
				bi.setMobnum(PhoneNumNideUtil.hideNum(bi.getMobnum()));
				list.set(i,bi);
			}
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());

			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void recharge(){
		
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		try {
			String message = this.service.recharge(paramsMap);
			JsonObject object = new JsonObject();
			object.addProperty("message", message);
			sendJsonMessage(object.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public String exportCVS() throws Exception {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		String value = service.exportCVS(paramsMap);
		input = new ByteArrayInputStream(value.getBytes("GBK"));
		fileName = System.currentTimeMillis() + ".csv";
		return ActionSupport.SUCCESS;
	}
	
	@Override
	public UserInfoSetModel getModel() {
		if (model == null) {
			model = new UserInfoSetModel();
		}
		return model;
	}

}
